<xsl:stylesheet version='1.0'
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:db="http://docbook.org/ns/docbook"
  xmlns:pbs="http://schema.pbs.gov.au"
  xmlns:dct="http://purl.org/dc/terms/">

  <xsl:import href='common.xsl'/>

  <db:article>
    <db:info>
      <db:title>Extract Fees File</db:title>

      <db:copyright>
        <db:year>2012</db:year>
        <db:year>2011</db:year>
        <db:year>2009</db:year>
        <db:holder>Commonwealth of Australia</db:holder>
      </db:copyright>

      <db:legalnotice>
        <db:para>This software is copyrighted by the Commonwealth of Australia ("The Commonwealth").  The following terms apply to all files associated with the software unless explicitly disclaimed in individual files.</db:para>
        <db:para>Permission is hereby granted to use, copy, modify, distribute, and license this software and its documentation for any purpose, provided that existing copyright notices are retained in all copies and that this notice is included verbatim in any distributions. No written agreement, license, or royalty fee is required for any of the authorized uses. Modifications to this software may be copyrighted by their authors and need not follow the licensing terms described here, provided that the new terms are clearly indicated on the first page of each file where they apply.</db:para>
        <db:para>IN NO EVENT SHALL THE COMMONWEALTH OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</db:para>
        <db:para>THE COMMONWEALTH SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE COMMONWEALTH HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.</db:para>
      </db:legalnotice>

      <db:author>
        <db:personname>Steve Ball</db:personname>
        <db:email>Steve.Ball@health.gov.au</db:email>
        <db:affiliation>
          <db:orgname>Department of Health and Ageing</db:orgname>
          <db:orgdiv>PharmBiz</db:orgdiv>
        </db:affiliation>
      </db:author>

      <db:revhistory>
        <db:revision>
          <db:revnumber>2.6.1</db:revnumber>
          <db:date>2012-11-07</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Added documentation.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.6</db:revnumber>
          <db:date>2012-11-05</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Exclude non-default Private Hospital Pharmacy and infusible dispensing rules.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.5</db:revnumber>
          <db:date>2012-10-23</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Improved display of markup bands.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.4</db:revnumber>
          <db:date>2012-05-16</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Use QNames as well as URIs. Use container-definition type instead of title. Fix display of markup bands.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.3</db:revnumber>
          <db:date>2011-11-22</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Fixed length of Drug Types field to be 43 instead of 54.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.2</db:revnumber>
          <db:date>2011-10-26</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Fixed safety net recording fees.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.1.1</db:revnumber>
          <db:date>2011-10-25</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Fixed extemp fee.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.1</db:revnumber>
          <db:date>2011-10-11</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Changed dispensing rule URI. If value missing, use "0.00".</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.0.1</db:revnumber>
          <db:date>2011-06-14</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Fixed extemp-extra fee.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.0</db:revnumber>
          <db:date>2011-02-11</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Updated for schema v2.4.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.0</db:revnumber>
          <db:date>2009-08-27</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Initial version.</db:revremark>
        </db:revision>
      </db:revhistory>
    </db:info>

    <db:para>This XSL stylesheet produces the Fees extract file (<db:filename>Fees yyyymmdd.txt</db:filename>) from a PBS XML document.</db:para>
    <db:para>The fees extract  file contains details of patient contributions, mark-ups and fees that are relevant to the prices of drugs shown in the file <db:filename>Drug yyyymmdd.txt</db:filename>.</db:para>
    <db:para>The source document must be a PBS XML document, schema-valid against version 2.4 (or later) of the PBS XML Schema.</db:para>
    <db:para>The result document is a text file.</db:para>
    <db:para><db:emphasis>It is a free format text file containing the fields listed below.</db:emphasis></db:para>
    <db:table>
      <db:title>Patient Contribution</db:title>
      <db:tgroup cols="4" align="left" colsep="1" rowsep="1">
        <db:colspec colnum="3" align="right"/>
        <db:thead>
          <db:row>
            <db:entry>Field Name</db:entry>
            <db:entry>Format</db:entry>
            <db:entry>Maximum Length</db:entry>
            <db:entry>Description</db:entry>
          </db:row>
        </db:thead>
        <db:tbody>
        <db:row>
            <db:entry>Code</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>6</db:entry>
            <db:entry>Values = C or G</db:entry>
          </db:row>
          <db:row>
            <db:entry>Contribution Type</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>25</db:entry>
            <db:entry>
              <db:para>Description of category code</db:para>
              <db:simplelist>
                <db:member>C = Concessional</db:member>
                <db:member>G = General</db:member>
              </db:simplelist>
            </db:entry>
          </db:row>
          <db:row>
            <db:entry>Maximum Contribution</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>15</db:entry>
            <db:entry>Maximum contribution towards  cost of prescription under the PBS</db:entry>
          </db:row>
          <db:row>
            <db:entry>Minimum Safety Net</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>15</db:entry>
            <db:entry>Minimum safety net value to  be recorded for prescriptions under PBS</db:entry>
          </db:row>
        </db:tbody>
      </db:tgroup>
    </db:table>
    <db:table>
      <db:title>Container Fee</db:title>
      <db:tgroup cols="4" align="left" colsep="1" rowsep="1">
        <db:colspec colnum="3" align="right"/>
        <db:thead>
          <db:row>
            <db:entry>Field Name</db:entry>
            <db:entry>Format</db:entry>
            <db:entry>Maximum Length</db:entry>
            <db:entry>Description</db:entry>
          </db:row>
        </db:thead>
        <db:tbody>
          <db:row>
            <db:entry>Code</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>6</db:entry>
            <db:entry>Values = I or O</db:entry>
          </db:row>
          <db:row>
            <db:entry>Container Type </db:entry>
            <db:entry>Character</db:entry>
            <db:entry>25</db:entry>
            <db:entry>
              <db:para>Description of Container Fee Code</db:para>
              <db:simplelist>
                <db:member>I  =  Injectable</db:member>
                <db:member>O = Other</db:member>
              </db:simplelist>
            </db:entry>
          </db:row>
          <db:row>
            <db:entry>Container Fee</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>15</db:entry>
            <db:entry>The ready-prepared container fee which applies to quantities less than the manufacturer's pack.  Not payable on quantities over the manufacturer's pack</db:entry>
          </db:row>
        </db:tbody>
      </db:tgroup>
    </db:table>
    <db:table>
      <db:title>Dispensing Fees</db:title>
      <db:tgroup cols="4" align="left" colsep="1" rowsep="1">
        <db:colspec colnum="3" align="right"/>
        <db:thead>
          <db:row>
            <db:entry>Field Name</db:entry>
            <db:entry>Format</db:entry>
            <db:entry>Maximum Length</db:entry>
            <db:entry>Description</db:entry>
          </db:row>
        </db:thead>
        <db:tbody>
          <db:row>
            <db:entry>Code</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>6</db:entry>
            <db:entry>Code to indicate Dispensing Fee Values = DD,EP,EW,NF,RP</db:entry>
          </db:row>
          <db:row>
            <db:entry>Fee Description</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>25</db:entry>
            <db:entry>
              <db:para>Description of Dispensing Fee Type Code</db:para>
              <db:simplelist>
                <db:member>DD = Dangerous Drug</db:member>
                <db:member>EP = Extemporaneously Prepared</db:member>
                <db:member>EW = Water added</db:member>
                <db:member>NF = No Fee</db:member>
                <db:member>RP = Ready Prepared</db:member>
              </db:simplelist>
            </db:entry>
          </db:row>
          <db:row>
            <db:entry>Dispensing Fee</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>15</db:entry>
            <db:entry>Dispensing fee for given Dispensing Type</db:entry>
          </db:row>
          <db:row>
            <db:entry>Additional Fee</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>15</db:entry>
            <db:entry>Additional fee for given Dispensing Type</db:entry>
          </db:row>
        </db:tbody>
      </db:tgroup>
    </db:table>
    <db:table>
      <db:title>Markups</db:title>
      <db:tgroup cols="4" align="left" colsep="1" rowsep="1">
        <db:colspec colnum="3" align="right"/>
        <db:thead>
          <db:row>
            <db:entry>Field Name</db:entry>
            <db:entry>Format</db:entry>
            <db:entry>Maximum Length</db:entry>
            <db:entry>Description</db:entry>
          </db:row>
        </db:thead>
        <db:tbody>
          <db:row>
            <db:entry>Code</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>6</db:entry>
            <db:entry>
              <db:para>one character alphabetic (current rates in file Fees yyyymmdd.txt), three values:</db:para>
              <db:simplelist>
                <db:member>A - no mark-up (section 100 drug types)</db:member>
                <db:member>C - standard six-tier ready-prepared mark-up; the price to pharmacist for maximum quantity determines which tier applies</db:member>
                <db:member>D - HSD four-tier ready-prepared mark-up; the price to pharmacist for maximum quantity determines which tier applies</db:member>
                <db:member>E - one-tier ready-prepared mark-up</db:member>
              </db:simplelist>
            </db:entry>
          </db:row>
          <db:row>
            <db:entry>Drug Types</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>43</db:entry>
            <db:entry>Drug types associated with a given Markup code (For drug type codes see separate table below at 6. Drug txt file)</db:entry>
          </db:row>
          <db:row>
            <db:entry>Markup Low Value</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>10</db:entry>
            <db:entry>The lower value of the Markup range</db:entry>
          </db:row>
          <db:row>
            <db:entry>Markup High Value</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>10</db:entry>
            <db:entry>The higer value of the Markup range</db:entry>
          </db:row>
          <db:row>
            <db:entry>Markup</db:entry>
            <db:entry>Character</db:entry>
            <db:entry>10</db:entry>
            <db:entry>This field may either be a percentage, indicated by a % sign following, or a fixed value, indicated by a preceding $ sign.</db:entry>
          </db:row>
        </db:tbody>
      </db:tgroup>
    </db:table>
  </db:article>

  <xsl:output method='text'/>
  
  <db:param>
    <db:refpurpose>QNames</db:refpurpose>
    <db:para>Whether to use QNames or expanded URIs.</db:para>
  </db:param>
  <xsl:param name='qnames' select='true()'/>
  
  <xsl:key name='band-by-code'
    match='/pbs:root/pbs:schedule/pbs:listings-list/pbs:dispensing-rules-list//pbs:markup-band[ancestor::pbs:listings-list/pbs:info/pbs:pricing-model != "http://schema.pbs.gov.au/Pricing/infusible"]'
    use='pbs:code'/>
  <xsl:key name="ident" match="*" use="@xml:id"/>

  <db:template name='root'>
    <db:refpurpose>Check Document Version</db:refpurpose>
    <db:para>Make sure that the source document uses the correct version of the PBS XML Schema.</db:para>
  </db:template>
  <xsl:template match='/'>
    <xsl:variable name='major' select='substring-before(/*/@version, ".")'/>

    <xsl:choose>
      <xsl:when test='$major >= 2'>
        <xsl:text>PATIENT CONTRIBUTION
--------------------
                                       maximum        minimum
code  contribution type           contribution     safety net
----  --------------------        ------------   ------------
C     Concessional                   $</xsl:text>
        <xsl:call-template name='pad-number'>
          <xsl:with-param name='number'
            select='format-number(/pbs:root/pbs:schedule/pbs:copayments-list/pbs:copayment[pbs:type = "copay:concessional"]/pbs:amount, "0.00")'/>
          <xsl:with-param name='length' select='8'/>
        </xsl:call-template>
        <xsl:text>      $    0.00
G     General                        $</xsl:text>
        <xsl:call-template name='pad-number'>
          <xsl:with-param name='number'
            select='format-number(/pbs:root/pbs:schedule/pbs:copayments-list/pbs:copayment[pbs:type = "copay:general"]/pbs:amount, "0.00")'/>
          <xsl:with-param name='length' select='8'/>
        </xsl:call-template>
        <xsl:text>      $    5.50&#xa;</xsl:text>
        <xsl:text>

CONTAINER FEES
--------------
                                     container
code  container type                       fee
----  --------------------        ------------
I     Injectable                     $</xsl:text>
          <xsl:call-template name='pad-number'>
          <xsl:with-param name='number'>
            <xsl:choose>
              <xsl:when test='/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:containers-list/pbs:container-definition[pbs:type = "container:injectable"]'>
                <xsl:value-of select='format-number(/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:containers-list/pbs:container-definition[pbs:type = "container:injectable"]/pbs:amount, "0.00")'/>
              </xsl:when>
              <xsl:otherwise>0.00</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name='length' select='8'/>
        </xsl:call-template>
        <xsl:text>&#xa;O     Other                          $</xsl:text>
        <xsl:call-template name='pad-number'>
          <xsl:with-param name='number'>
            <xsl:choose>
              <xsl:when test='/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:containers-list/pbs:container-definition[pbs:type = "container:other"]'>
                <xsl:value-of select='format-number(/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:containers-list/pbs:container-definition[pbs:type = "container:other"]/pbs:amount, "0.00")'/>
              </xsl:when>
              <xsl:otherwise>0.00</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name='length' select='8'/>
        </xsl:call-template>
        <xsl:text>&#xa;</xsl:text>
        <xsl:text>

DISPENSING FEES
---------------
                                    dispensing     additional
code  fee description                      fee            fee
----  ---------------               ----------     ----------&#xa;
DD    Dangerous Drug                $</xsl:text>
        <xsl:call-template name='pad-number'>
          <xsl:with-param name='number'>
            <xsl:choose>
              <xsl:when test='/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:dangerous-drug"]'>
                <xsl:value-of select='format-number(/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:dangerous-drug"]/pbs:amount, "0.00")'/>
              </xsl:when>
              <xsl:otherwise>0.00</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name='length' select='9'/>
        </xsl:call-template>
        <xsl:text>     $</xsl:text>
        <xsl:call-template name='pad-number'>
          <xsl:with-param name='number'>
            <xsl:choose>
              <xsl:when test='/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:dangerous-drug-extra"]'>
                <xsl:value-of select='format-number(/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:dangerous-drug-extra"]/pbs:amount, "0.00")'/>
              </xsl:when>
              <xsl:otherwise>0.00</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name='length' select='9'/>
        </xsl:call-template>
        <xsl:text>
EP    Extemp Prepared               $</xsl:text>
        <xsl:call-template name='pad-number'>
          <xsl:with-param name='number'>
            <xsl:choose>
              <xsl:when test='/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:extemp"]'>
                <xsl:value-of select='format-number(/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:dispensing"]/pbs:amount +
                  /pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:extemp"]/pbs:amount, "0.00")'/>
              </xsl:when>
              <xsl:otherwise>0.00</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name='length' select='9'/>
        </xsl:call-template>
        <xsl:text>     $</xsl:text>
        <xsl:call-template name='pad-number'>
          <xsl:with-param name='number'>
            <xsl:choose>
              <xsl:when test='/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:safety-net-recording-extemp"]'>
                <xsl:value-of select='format-number(/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:safety-net-recording-extemp"]/pbs:amount, "0.00")'/>
              </xsl:when>
              <xsl:otherwise>0.00</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name='length' select='9'/>
        </xsl:call-template>
        <xsl:text>
EW    Water added                   $</xsl:text>
        <xsl:call-template name='pad-number'>
          <xsl:with-param name='number'>
            <xsl:choose>
              <xsl:when test='/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:water-added"]'>
                <xsl:value-of select='format-number(/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:dispensing"]/pbs:amount +
                  /pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:extemp"]/pbs:amount +
                  /pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:water-added"]/pbs:amount, "0.00")'/>
              </xsl:when>
              <xsl:otherwise>0.00</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name='length' select='9'/>
        </xsl:call-template>
        <xsl:text>     $</xsl:text>
        <xsl:call-template name='pad-number'>
          <xsl:with-param name='number'>
            <xsl:choose>
              <xsl:when test='/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:safety-net-recording-extemp"]'>
                <xsl:value-of select='format-number(/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:safety-net-recording-extemp"]/pbs:amount, "0.00")'/>
              </xsl:when>
              <xsl:otherwise>0.00</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name='length' select='9'/>
        </xsl:call-template>
        <xsl:text>
NF    No Fee                        $     0.00     $     0.00
RP    Ready Prepared                $</xsl:text>
        <xsl:call-template name='pad-number'>
          <xsl:with-param name='number'>
            <xsl:choose>
              <xsl:when test='/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:dispensing"]'>
                <xsl:value-of select='format-number(/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:dispensing"]/pbs:amount, "0.00")'/>
              </xsl:when>
              <xsl:otherwise>0.00</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name='length' select='9'/>
        </xsl:call-template>
        <xsl:text>     $</xsl:text>
        <xsl:call-template name='pad-number'>
          <xsl:with-param name='number'>
            <xsl:choose>
              <xsl:when test='/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:safety-net-recording"]'>
                <xsl:value-of select='format-number(/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:code = "GE"]/pbs:dispensing-rules-list/pbs:dispensing-rule[@about = $dr.rp.cp or @about = $dr.rp.cp.qn]/pbs:fees-list/pbs:fee-definition[pbs:type = "fee:safety-net-recording"]/pbs:amount, "0.00")'/>
              </xsl:when>
              <xsl:otherwise>0.00</xsl:otherwise>
            </xsl:choose>
          </xsl:with-param>
          <xsl:with-param name='length' select='9'/>
        </xsl:call-template>
        <xsl:text>

MARKUPS
-------

code  drug types                                       range                     markup
----  ------------------------------------------- ----------    ----------   ----------&#xa;</xsl:text>

        <xsl:apply-templates select='/pbs:root/pbs:schedule/pbs:listings-list[pbs:info/pbs:pricing-model != "http://schema.pbs.gov.au/Pricing/infusible"]/pbs:dispensing-rules-list/pbs:dispensing-rule/pbs:markups-list/pbs:markup-band[pbs:code != "W"][count(.|key("band-by-code", pbs:code)[1]) = 1]'
            mode='markup'>
            <xsl:sort select='pbs:code'/>
          </xsl:apply-templates>

      </xsl:when>
      <xsl:otherwise>
        <xsl:message terminate='yes'>This stylesheet is designed to operate upon a Schedule XML document that conforms to version 2.4 (or later) of the PBS XML Schema.  See http://xml.pbs.gov.au/ for more information on the PBS XML Schema, and for a version of this XSL stylesheet that operates on documents that conform to an earlier version of the schema.</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match='pbs:markup-band' mode='markup'>
    <!-- found a markup band, but must make sure it is in the default dispensing rule -->
    <xsl:variable name="default">
      <xsl:for-each select="key('band-by-code', pbs:code)">
        <xsl:variable name="dr.dflt">
          <xsl:call-template name="get-default-dispensing-rule">
            <xsl:with-param name="qnames" select="$qnames"/>
            <xsl:with-param name="program" select="ancestor::pbs:listings-list/pbs:info/pbs:code"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:if test="ancestor::pbs:dispensing-rule/@about = $dr.dflt and
          (pbs:code != 'A' or not(../pbs:markup-band[pbs:code != 'W' and pbs:code != 'A']))">
          <xsl:text> </xsl:text>
          <xsl:value-of select="@xml:id"/>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="contains($default, @xml:id)">
        <xsl:apply-templates select="." mode="markup.dflt"/>
      </xsl:when>
      <xsl:when test="contains(normalize-space($default), ' ')">
        <xsl:apply-templates select="key('ident', substring-before(normalize-space($default), ' '))" mode="markup.dflt"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="key('ident', normalize-space($default))" mode="markup.dflt"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="pbs:markup-band" mode="markup.dflt">
    <xsl:apply-templates select='.|following-sibling::pbs:markup-band[pbs:code = current()/pbs:code]' mode='markup-bands'/>
  </xsl:template>
  <xsl:template match='pbs:markup-band' mode='markup-bands'>
    <xsl:value-of select='pbs:code'/>
    <xsl:text>     </xsl:text>
    <xsl:variable name="programs">
      <xsl:for-each select="key('band-by-code', pbs:code)">
        <xsl:variable name="dr.dflt">
          <xsl:call-template name="get-default-dispensing-rule">
            <xsl:with-param name="qnames" select="$qnames"/>
            <xsl:with-param name="program" select="ancestor::pbs:listings-list/pbs:info/pbs:code"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:if test="ancestor::pbs:dispensing-rule/@about = $dr.dflt and
          (pbs:code != 'A' or not(../pbs:markup-band[pbs:code != 'W' and pbs:code != 'A']))">
          <xsl:text>, </xsl:text>
          <xsl:value-of select="ancestor::pbs:listings-list/pbs:info/pbs:code"/>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="programs.cooked">
      <xsl:call-template name="remove-duplicates">
        <xsl:with-param name="text" select="substring-after($programs, ', ')"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:call-template name='pad-right'>
      <xsl:with-param name='length' select='43'/>
      <xsl:with-param name='text' select="substring-after($programs.cooked, ', ')"/>
    </xsl:call-template>
    <xsl:text> $</xsl:text>
    <xsl:call-template name='pad-number'>
      <xsl:with-param name='length' select='9'/>
      <xsl:with-param name='number' select='format-number(pbs:limit, "0.00")'/>
    </xsl:call-template>
    <xsl:text> to $</xsl:text>
    <xsl:call-template name='pad-number'>
      <xsl:with-param name='length' select='9'/>
      <xsl:with-param name='number'>
        <xsl:choose>
          <xsl:when test='following-sibling::pbs:markup-band[pbs:code = current()/pbs:code]'>
            <xsl:value-of select='format-number(following-sibling::pbs:markup-band[1]/pbs:limit - 0.01, "0.00")'/>
          </xsl:when>
          <xsl:otherwise>99999.99</xsl:otherwise>
        </xsl:choose>
      </xsl:with-param>
    </xsl:call-template>
    <xsl:text>   </xsl:text>
    <xsl:choose>
      <xsl:when test='pbs:type = "markup:dollar"'>
        <xsl:text>$</xsl:text>
        <xsl:call-template name='pad-number'>
          <xsl:with-param name='number' select='format-number(pbs:amount, "0.00")'/>
          <xsl:with-param name='length' select='8'/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name='pad-number'>
          <xsl:with-param name='number' select='format-number(pbs:amount, "0.00")'/>
          <xsl:with-param name='length' select='9'/>
        </xsl:call-template>
        <xsl:text> %</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>
  <xsl:template name="remove-duplicates">
    <xsl:param name="text"/>
    <xsl:param name="accumulated"/>
    <xsl:choose>
      <xsl:when test="not($text)">
        <xsl:value-of select="$accumulated"/>
      </xsl:when>
      <xsl:when test="not(contains($text, ',')) and
        not(contains($accumulated, $text))">
        <xsl:value-of select="concat($accumulated, ', ', $text)"/>
      </xsl:when>
      <xsl:when test="not(contains($text, ','))">
        <xsl:value-of select="$accumulated"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="first" select="substring-before($text, ',')"/>
        <xsl:variable name="rest" select="normalize-space(substring-after($text, ','))"/>

        <xsl:choose>
          <xsl:when test="contains($accumulated, $first)">
            <xsl:call-template name="remove-duplicates">
              <xsl:with-param name="text" select="$rest"/>
              <xsl:with-param name="accumulated" select="$accumulated"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="remove-duplicates">
              <xsl:with-param name="text" select="$rest"/>
              <xsl:with-param name="accumulated" select="concat($accumulated, ', ', $first)"/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

